package com.sskj.bibi.presenter;

import com.google.gson.Gson;
import com.sskj.bibi.ui.fragment.PankouUpDownFragment;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.common.util.DisposUtil;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.lib.http.CallBackOption;
import com.sskj.lib.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;


public class PankouUpDownFragmentPresenter extends BasePresenter<PankouUpDownFragment> {

    private WebSocketObserver stockSocketFive;
    private Disposable stockDisposableFive;
    private String code;

    public void getProduct(String code) {
        httpService.getProduct(null,code)
                .execute(new CallBackOption<HttpData<List<CoinBean>>>() {
                }.unLoadBind(mView)
                        .execute(httpData -> {
                            if (httpData.getData() != null && httpData.getData().size() > 0) {
                                Flowable.fromIterable(httpData.getData())
                                        .filter(stockCoinBean -> stockCoinBean.getCode().equals(code))
                                        .subscribe(productBean -> {
                                            mView.requestCoin(httpData.getData().get(0));
                                        }, e -> {
                                        });
                            }
                }));
    }

    public void getPankou(String code) {
        httpService.getPankou(code)
                .execute(new CallBackOption<HttpData<WSFiveBean>>() {
                }.unLoadBind(mView)
                        .execute(httpData->{
                            if (httpData.getData() != null) {
                                WSFiveBean wsFiveBean = httpData.getData();
                                mView.updateFive(wsFiveBean);
                            } else {
                                WSFiveBean wsFiveBean = new WSFiveBean();
                                wsFiveBean.setCode(code);
                                wsFiveBean.setAsks(new ArrayList<>());
                                wsFiveBean.setBids(new ArrayList<>());
                                mView.updateFive(wsFiveBean);
                            }
                        }));
    }



    public void initSocket(String code) {
        if (code==null)
            return;
        code= CommonUtil.dealReuqestCode(code);
        if (stockSocketFive == null) {
            stockSocketFive = httpService.pushCoinFive(code);
            stockDisposableFive = stockSocketFive
                    .subscribe(s -> {
                        System.out.println(s);
                    });
//                    .map(s -> new Gson().fromJson(s, WSFiveBean.class))
//                    .subscribe(data -> mView.updateFive(data));
        } else {
            stockSocketFive.send("{\"type\":\"buy_sell_five\",\"list\":[\""+code+"\"]}");
        }
        this.code=code;
    }

    @Override
    public void detachView() {
        closeWebSocket();
        super.detachView();
    }

    public void closeWebSocket() {
        if (stockSocketFive!=null) {
            stockSocketFive.close();
            stockSocketFive = null;
        }
        DisposUtil.close(stockDisposableFive);
    }
}
